home *** CD-ROM | disk | FTP | other *** search
- /*
- * basic layout sample.c
- * By Eric Mader after Robert Dierkes
-
- 1.0B2 (MD, 09/09/93) -- change QDLocalToFixedGlobal to GXConvertQDPoint, including
- removing validation just for B2
- */
-
- /* Copyright ©1989, 1990, 1991, 1992 Apple Computer, Inc. All rights reserved. */
-
- /*----------------*/
- /* Include Files */
- /*----------------*/
- #ifndef THINK_C
- #include <Types.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Events.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <Desk.h>
- #include <Script.h>
- #include <ToolUtils.h>
- #include <OSEvents.h>
- #include <SegLoad.h>
- #define thePort qd.thePort
- #define screenBits qd.screenBits
- #endif
-
- #include "graphics toolbox.h"
- #include "graphics libraries.h"
- #include "graphics debugging.h"
- #include "qd library.h"
-
- #include "layout feature constants.h"
- #include "layout library.h"
- #include "font routines.h"
- #include "font library.h"
- #include "font menu library.h"
- #include "layout edit library.h"
-
-
- /*----------------*/
- /* Menu Bar */
- /*----------------*/
- enum
- {
- appleMenuID = 128,
- fileMenuID,
- editMenuID,
- fontMenuID,
- nestingMenuID
- };
-
- enum
- {
- /* Apple Menu */
- aboutCommand = 1,
-
- /* File Menu */
- quitCommand = 1,
-
- /* Edit Menu */
- undoCommand = 1,
- dash0Command,
- cutCommand,
- copyCommand,
- pasteCommand,
- clearCommand,
-
- /* nesting menu */
- zeroCommand = 1,
- oneCommand,
- twoCommand,
- threeCommand,
- fourCommand,
- fiveCommand,
- sixCommand,
- sevenCommand,
- eightCommand,
- nineCommand,
- tenCommand,
- elevenCommand,
- twelveCommand,
- thirteenCommand,
- fourteenCommand,
- fifteenCommand,
- dash1Command,
- nestCommand,
- unnestCommand
-
-
- };
-
- /*----------------*/
- /* About Box */
- /*----------------*/
- enum
- {
- aboutDLOG = 128,
-
- okButton = 1,
- showsItem,
- authorItem
- };
-
-
- /*------------------*/
- /* Defines */
- /*------------------*/
- #define inch 72
- #define halfInch 36
- #define quarterInch 18
-
- #define windowOnTop ((WindowPtr)-1)
- #define margin quarterInch
- #define windowTitleHt 20
- #define windowWidth 9*inch
- #define windowHeight 4*inch
- #define windowTitle "\p\024 Basic Layout Sample"
- #define noRefCon (long) 0
-
- #define hiliteAllMenus (short) 0
-
- typedef struct {
- gxFont fontID;
- gxStyle fontStyle;
- } FontMenuData;
-
- /*----------------------*/
- /* Global Declarations */
- /*----------------------*/
- Boolean quitApp;
- MenuHandle appleMenu, fileMenu, editMenu, fontMenu, nestingMenu;
- short fontMenuCount;
- FontMenuData *fontMenuData;
- WindowPtr layoutWindow;
- Rect dragRect;
- gxShape eraser;
-
-
- static void InitMacintosh(void)
- {
- MaxApplZone();
- MoreMasters();
- MoreMasters();
-
- /* gxInitialize graphics system */
- GXEnterGraphics();
- SetGraphicsLibraryErrors();
- SetGraphicsLibraryNotices();
- InitCommonColors();
-
- /* gxInitialize other toolbox managers */
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- InitCursor();
-
- } /* InitMacintosh */
-
-
- static void ShowAboutBox(void)
- { GrafPtr savePort;
- DialogPtr theDialog;
- short itemType;
- Handle itemHdl;
- Rect itemRect;
- short itemHit;
-
- GetPort(&savePort);
- theDialog = GetNewDialog(aboutDLOG, nil, (WindowPtr) -1);
- SetPort(theDialog);
-
- GetDItem(theDialog, authorItem, &itemType, &itemHdl, &itemRect);
- SetIText(itemHdl, (StringPtr) "\pDr. Strangemorph");
- GetDItem(theDialog, showsItem, &itemType, &itemHdl, &itemRect);
- SetIText(itemHdl, (StringPtr) "\pbasic layout");
-
- do
- {
- ModalDialog(nil, &itemHit);
- }
- while (itemHit != okButton);
-
- CloseDialog(theDialog);
-
- SetPort(savePort);
-
- } /* ShowAboutBox */
-
-
- static gxStyle FontMenuItemStyle(short fontItem)
- { FontMenuData *fmd = fontMenuData;
- gxFont fontID;
- Str255 gxFontName;
-
- GetItem(fontMenu, fontItem, gxFontName);
-
- fontID = FindPNameFont(gxFullFontName, gxFontName);
-
- while (fmd->fontStyle)
- { if (fmd->fontID == fontID) return fmd->fontStyle;
- ++fmd;
- }
-
- fmd->fontID = fontID;
- fmd->fontStyle = GXNewStyle();
- GXSetStyleFont(fmd->fontStyle, fontID);
- GXSetStyleTextSize(fmd->fontStyle, ff(40));
-
- return fmd->fontStyle;
- }
-
- static void DisposeFontMenu(void)
- { FontMenuData *fmd = fontMenuData;
-
- while (fmd->fontStyle) GXDisposeStyle(fmd++->fontStyle);
-
- DisposPtr((Ptr) fontMenuData);
- }
-
- static void SetUpMenus(void)
- { short i;
- FontMenuData *fmd;
-
- InsertMenu(appleMenu = GetMenu(appleMenuID), 0);
- AddResMenu(appleMenu, (ResType) 'DRVR');
- InsertMenu(fileMenu = GetMenu(fileMenuID), 0);
- InsertMenu(editMenu = GetMenu(editMenuID), 0);
- EnableItem(GetMHandle(editMenuID), cutCommand);
- EnableItem(GetMHandle(editMenuID), copyCommand);
- EnableItem(GetMHandle(editMenuID), pasteCommand);
- EnableItem(GetMHandle(editMenuID), clearCommand);
-
- fontMenu = NewMenu(fontMenuID, (StringPtr) "\pStyle");
- FontMenu(fontMenu);
- InsertMenu(fontMenu, 0);
-
- fontMenuCount = CountMItems(fontMenu);
- fontMenuData = (FontMenuData *) NewPtr(fontMenuCount * sizeof (FontMenuData));
-
- for (fmd = fontMenuData, i = fontMenuCount - 1; i >= 0; --i, ++fmd) fmd->fontStyle = nil;
-
- nestingMenu = NewMenu(nestingMenuID, (StringPtr) "\pNesting");
- AppendMenu(nestingMenu, (StringPtr) "\p0;1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;(-;Nest;UnNest");
- InsertMenu(nestingMenu, 0);
-
- DrawMenuBar();
-
- } /* SetUpMenus */
-
-
- static void SetUpWindow(void)
- { Rect windowBounds;
- gxViewPort myPort;
-
- SetRect(&windowBounds,
- margin,
- GetMBarHeight() + margin + windowTitleHt,
- windowWidth - margin,
- windowHeight - margin);
-
- dragRect = screenBits.bounds;
-
- layoutWindow = NewWindow(
- nil,
- &windowBounds,
- (StringPtr) windowTitle,
- true,
- zoomDocProc,
- windowOnTop,
- true,
- noRefCon);
-
- myPort = GXNewWindowViewPort(layoutWindow);
- SetDefaultViewPort(myPort);
- } /* SetUpWindow */
-
-
- static void UpdateAppWindow(WindowPtr theWindow)
- {
- InvalRect(&theWindow->portRect);
-
- } /* UpdateAppWindow */
-
-
- static void DoMenuCommand(LayoutEditHandle layoutHandle, long menuResult)
- { short menuID, itemNumber;
- GrafPtr savePort;
- Str255 daName;
-
- if (! menuResult)
- return;
-
- menuID = HiWord(menuResult);
- itemNumber = LoWord(menuResult);
-
- switch (menuID)
- {
- case appleMenuID:
- switch (itemNumber)
- {
- case aboutCommand:
- ShowAboutBox();
- break;
-
- default:
- GetItem(appleMenu, itemNumber, daName);
- GetPort(&savePort);
- (void) OpenDeskAcc(daName);
- SetPort(savePort);
- break;
- }
- break;
-
- case fileMenuID:
- switch(itemNumber)
- {
- case quitCommand:
- quitApp = true;
- break;
-
- default:
- break;
- }
- break;
-
- case editMenuID:
- switch(itemNumber)
- { case cutCommand:
- LayoutEditCut(layoutHandle);
- break;
-
- case copyCommand:
- LayoutEditCopy(layoutHandle);
- break;
-
- case pasteCommand:
- LayoutEditPaste(layoutHandle);
- break;
-
- case clearCommand:
- LayoutEditClear(layoutHandle);
- break;
- }
- break;
-
- case fontMenuID:
- { gxStyle menuStyle = FontMenuItemStyle(itemNumber);
-
- if (menuStyle) LayoutEditSetStyle(layoutHandle, menuStyle);
- break;
- }
-
- case nestingMenuID:
- switch (itemNumber)
- { case nestCommand:
- LayoutEditIncrementLevel(layoutHandle);
- break;
-
- case unnestCommand:
- LayoutEditDecrementLevel(layoutHandle);
- break;
-
- default:
- LayoutEditSetLevel(layoutHandle, itemNumber - zeroCommand);
- break;
- }
- break;
-
- default:
- break;
- } /* switch (menuID) */
-
- HiliteMenu(hiliteAllMenus);
-
- } /* DoMenuCommand */
-
-
- static void DoGrowBox(EventRecord *pEvent, WindowPtr theWindow)
- { long newSize;
-
- if (newSize = GrowWindow(theWindow, pEvent->where, &screenBits.bounds))
- {
- SizeWindow(theWindow, (short) newSize, (short) (newSize >> 16), true);
- UpdateAppWindow(theWindow);
- }
-
- } /* DoGrowBox */
-
-
- static void DoZoomBox(EventRecord *pEvent, WindowPtr theWindow, short windowPart)
- {
- if (TrackBox(theWindow, pEvent->where, windowPart))
- {
- ZoomWindow(theWindow, windowPart, true);
- UpdateAppWindow(theWindow);
- }
-
- } /* DoZoomBox */
-
-
- static void DoNullEvent(LayoutEditHandle layoutHandle)
- {
- LayoutEditIdle(layoutHandle);
- } /* DoNullEvent */
-
-
- static void DoMouseDown(LayoutEditHandle layoutHandle, EventRecord *pEvent)
- { short windowPart;
- WindowPtr whichWindow;
- gxValidationLevel currentValidation;
-
- windowPart = FindWindow(pEvent->where, &whichWindow);
-
- switch (windowPart)
- {
- case inDesk:
- break;
-
- case inMenuBar:
- DoMenuCommand(layoutHandle, MenuSelect(pEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(pEvent, whichWindow);
- break;
-
- case inContent:
- if (whichWindow != FrontWindow())
- SelectWindow(whichWindow);
- else
- { gxPoint hitDown;
-
- //
- // Unfortunately, GXConvertQDPoint(..) will not survive validation with the QD GX ß2
- // seed. Therefore, we get the validation level set up by the application, and turn the
- // validation off. We will reset the validation to it's original setting below.
- //
- // This problem has already been fixed in the QD GX ß3 build, therefore this work around
- // will be removed at ß3. Details about the parameters for the GXConvertQDPoint (..) call
- // can be found in the QD GX ß2 Graphics Notes within the "• Open Me First •" folder.
- //
- currentValidation = GXGetValidation();
- if (currentValidation) GXSetValidation(gxNoValidation);
-
- /** Convert the global Quickdraw coordinates to local fixed coordinates. **/
- GXConvertQDPoint(&pEvent->where, 0, &hitDown);
-
- //
- // Reset the validation to the original setting.
- //
- if (currentValidation) GXSetValidation(currentValidation);
-
- LayoutEditClick(layoutHandle, hitDown);
- }
- break;
-
- case inDrag:
- if (whichWindow == layoutWindow)
- DragWindow(layoutWindow, pEvent->where, &dragRect);
- break;
-
- case inGrow:
- DoGrowBox(pEvent, whichWindow);
- break;
-
- case inGoAway:
- if (whichWindow == layoutWindow &&
- TrackGoAway(layoutWindow, pEvent->where))
- quitApp = true;
- break;
-
- case inZoomIn:
- case inZoomOut:
- DoZoomBox(pEvent, whichWindow, windowPart);
- break;
-
- default:
- break;
- } /* switch */
-
- } /* DoMouseDown */
-
-
- static void DoKeyStroke(LayoutEditHandle layoutHandle, EventRecord *pEvent)
- { char charCode;
-
- charCode = pEvent->message & charCodeMask;
-
- if (pEvent->modifiers & btnState)
- {
- /*--------------*/
- /* Button is UP */
- /*--------------*/
- if (pEvent->modifiers & cmdKey)
- {
- /*--------------*/
- /* Command key */
- /*--------------*/
- DoMenuCommand(layoutHandle, MenuKey(charCode));
- }
- else LayoutEditKey(layoutHandle, charCode);
- }
-
- } /* DoKeyStroke */
-
-
- static void DoUpdate(LayoutEditHandle layoutHandle, EventRecord *pEvent)
- { GrafPtr savedPort;
- WindowPtr pUpdateWindow;
-
- GetPort(&savedPort);
-
- pUpdateWindow = (WindowPtr) pEvent->message;
-
- SetPort(pUpdateWindow);
- BeginUpdate(pUpdateWindow);
- GXDrawShape(eraser);
-
- LayoutEditUpdate(layoutHandle);
-
- EndUpdate(pUpdateWindow);
- SetPort(savedPort);
-
- } /* DoUpdate */
-
-
- static void DoActivate(LayoutEditHandle layoutHandle, EventRecord *pEvent)
- { WindowPtr pActiveWindow;
-
- /*--------------------------------------*/
- /* Get the window to de/activate */
- /* and its kind from the event message */
- /*--------------------------------------*/
- pActiveWindow = (WindowPtr) pEvent->message;
-
- /*-----------------*/
- /* Activate window */
- /*-----------------*/
- if (pEvent->modifiers & activeFlag)
- {
- /* Enable/Disable items */
- SetPort(pActiveWindow);
- LayoutEditActivate(layoutHandle);
- LayoutEditFromScrap(layoutHandle);
- }
- else
- /*-------------------*/
- /* Deactivate window */
- /*-------------------*/
- {
- /* Enable/Disable items */
- LayoutEditToScrap(layoutHandle);
- LayoutEditDeactivate(layoutHandle);
- }
-
- } /* DoActivate */
-
-
- static void DoEvent(LayoutEditHandle layoutHandle)
- { EventRecord theEvent;
-
- SystemTask();
- GetNextEvent(everyEvent, &theEvent);
-
- switch (theEvent.what)
- {
- case nullEvent:
- DoNullEvent(layoutHandle);
- break;
-
- case mouseDown:
- DoMouseDown(layoutHandle, &theEvent);
- break;
-
- case mouseUp:
- break;
-
- case keyDown:
- case autoKey:
- DoKeyStroke(layoutHandle, &theEvent);
- break;
-
- case keyUp:
- break;
-
- case updateEvt:
- DoUpdate(layoutHandle, &theEvent);
- break;
-
- case activateEvt:
- DoActivate(layoutHandle, &theEvent);
- break;
-
- case diskEvt:
- case networkEvt:
- case driverEvt:
- default:
- break;
- } /* switch (theEvent.what) */
-
- } /* DoEvent */
-
-
- static short StrLength(char *s)
- {short len;
-
- for (len = 0; *s++ != 0; len++) ;
- return len;
- }
-
- void main(void)
- { LayoutEditHandle layoutHandle;
- gxRunControls gxRunControls;
- gxLayoutOptions gxLayoutOptions;
- gxStyle hoefler, times, baghdad;
- char *text1 = "The wicked ";
- char *text2 = "fast ";
- /* The following is "Macintosh" in Arabic: */
- /* meem, alif, kaf, noon, tah, wau, shin, */
- static char text3[] = {0xE5, 0xC7, 0xE3, 0xE6, 0xCA, 0xE8, 0xD4, 0};
- char *text4 = " lives!";
- char *textRuns[4];
- short level = 0, textLengths[4], totalLength;
- gxStyle textStyles[4];
- gxPoint posn = {ff(halfInch + quarterInch), ff(inch + halfInch)};
-
- InitMacintosh();
- SetUpMenus();
- SetUpWindow();
-
- eraser = GXNewShape(gxFullType);
- SetShapeCommonColor(eraser, gxWhite);
-
- /*-------------------------------------*/
- /* Make a canned layout to start with */
- /*-------------------------------------*/
-
- /* gxInitialize the textRuns array */
- textRuns[0] = text1;
- textRuns[1] = text2;
- textRuns[2] = text3;
- textRuns[3] = text4;
-
- /* gxInitialize the textLengths array */
- textLengths[0] = StrLength(text1);
- textLengths[1] = StrLength(text2);
- textLengths[2] = StrLength(text3);
- textLengths[3] = StrLength(text4);
-
- totalLength = textLengths[0] + textLengths[1] + textLengths[2] + textLengths[3];
-
- /* Make a default gxLayoutOptions and gxRunControls */
- InitializeLayoutOptions (&gxLayoutOptions);
- InitializeRunControls (&gxRunControls);
-
- hoefler = NewLayoutStyle(
- (char *) "\pHoefler Italic",
- ff(40),
- 0,
- &gxRunControls,
- nil,
- 0,
- nil);
-
- times = NewLayoutStyle(
- (char *) "\pTimes Roman",
- ff(40),
- 0,
- &gxRunControls,
- nil,
- 0,
- nil);
-
- baghdad = NewLayoutStyle(
- (char *) "\pBaghdad Plain",
- ff(40),
- 0,
- &gxRunControls,
- nil,
- 0,
- nil);
-
- textStyles[0] = hoefler;
- textStyles[1] = times;
- textStyles[2] = baghdad;
- textStyles[3] = hoefler;
-
- layoutHandle = NewLayoutEditHandle(
- 4,
- textLengths,
- (void *) textRuns,
- 4,
- textLengths,
- textStyles,
- 1,
- &totalLength,
- &level,
- &gxLayoutOptions,
- &posn,
- hoefler);
-
- GXDisposeStyle(hoefler);
- GXDisposeStyle(times);
- GXDisposeStyle(baghdad);
-
- while (!quitApp)
- DoEvent(layoutHandle);
-
- GXDisposeShape(eraser);
- DisposeWindow(layoutWindow);
- DisposeLayoutEditHandle(layoutHandle);
- DisposeCommonColors();
- DisposeFontMenu();
- GXExitGraphics();
- ExitToShell();
-
- } /* main */
-